package pt.inescid.components.policyengine.heimdall.pap.base;

import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.EventObj;
import pt.inescid.components.policyengine.heimdall.base.EventRelevance;
import pt.inescid.components.policyengine.heimdall.base.User;
import pt.inescid.components.policyengine.heimdall.commons.Utilities;

public class EventFieldDefinition extends SimpleDefinition {

	private ModelDefinition modelDef;
	
	public EventFieldDefinition(
			ModelDefinition modelDef,
			int type, 
			String valueType, 
			RuleDefinition previousLevel, 
			String value) {
		super(type, valueType, previousLevel, value);
		
		this.modelDef = modelDef;
	}


	//  ----------------------------------------------------------
//  ------------------------   getFunction   ------------------------
//  ----------------------------------------------------------
	public String getFunction() {
		String globalVar = value;
		String currentField = null;
		String result = "";		
		
		if (getPreviousLevel() != null && this.getPreviousLevel().getType() == 
					RuleDefinition.CES_SUM_EXPRESSION) {
			result += 
"			EventObj " + getTemporaryVar() + "A = (EventObj) " +
this.getPreviousLevel().getTemporaryVar() + "B.elementAt(i);\n";	
		}
		else {
			result += 
"		EventObj " + getTemporaryVar() + "A = currentModel.getNextQuantifierEvent(\n" +
"				currentPolicyName, \"" + 
value.substring(0, value.indexOf(".")) + "\");\n";	
		}		
		
		result += 
"		" + getTypeDeclaration(valueType) + 
getTemporaryVar() + " = " + getTemporaryVar() + "A";
		globalVar = globalVar.substring(globalVar.indexOf(".")+1);
		
		while (globalVar.contains(".")) {
			currentField = globalVar.substring(0, globalVar.indexOf("."));
			result += ".get" + currentField.substring(0, 1).toUpperCase() + 
					currentField.substring(1) + "()";
			globalVar = globalVar.substring(globalVar.indexOf(".")+1);
		}
		
		result += ".get" + globalVar.substring(0, 1).toUpperCase() + 
					globalVar.substring(1) + "();\n";

		return (result);
	}

//  ----------------------------------------------------------
//  ------------------------   getRelevantEventFields   ------------------------
//  ----------------------------------------------------------
	public Vector getRelevantEventFields() {
		if (value.startsWith("ce.")) {
			return (null);
		}
		
		EventRelevance newField = new EventRelevance(
				modelDef,
				null,
				null,
				value,
				valueType,
				false);
		
		Vector result = new Vector();
		result.add(newField);
		
		return (result);
	} // getRelevantEventFields

	
//  ----------------------------------------------------------
//  ------------------------   getParameters   ------------------------
//  ----------------------------------------------------------
	public Vector getParameters() {
		Vector allParameters = new Vector();
		
		allParameters.add("EventObj " + 
				value.substring(0, value.indexOf(".")));
		
		return (allParameters);
	} // getParameters

	//  ----------------------------------------------------------
//  ------------------------   isPolicyComposition   ------------------------
//  ----------------------------------------------------------
	public boolean isPolicyComposition() {
		return (false);
	}

	
//  ----------------------------------------------------------
//  ------------------------   getTimeIndependentCopy   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getTimeIndependentCopy() {
		if (value.endsWith("Time")) {
			return (null);
		}
		else {
			EventFieldDefinition result = new EventFieldDefinition(
					modelDef,
					type, 
					RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
					previousLevel, value);
			
			return (result);
		}
	} // getTimeIndependentCopy


	
//  ----------------------------------------------------------
//  ------------------------   getObligationIndependentCopy   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getObligationIndependentCopy() {
		if (value.startsWith("fe")) {
			return (null);
		}
		else {
			EventFieldDefinition result = new EventFieldDefinition(
					modelDef,
					type, 
					RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
					previousLevel, value);
			
			return (result);
		}
	} // getObligationIndependentCopy


//  ----------------------------------------------------------
//  ------------------------   getObligation   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getObligation() {
		String newValue = value;
		
		EventFieldDefinition result = new EventFieldDefinition(
				modelDef,
				type, 
				RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
				previousLevel, newValue);
		
		return (result);
	} // getObligation


//  ----------------------------------------------------------
//  ------------------------   isObligationDependent   ------------------------
//  ----------------------------------------------------------
	public boolean isObligationDependent() {
		return (value.startsWith("fe"));
	} // isObligationDependent



//  ----------------------------------------------------------
//  ------------------------   getCeIndependentCopy   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition getCeIndependentCopy() {
		if (value.startsWith("ce.")) {
			return (null);
		}
		else {
			EventFieldDefinition result = new EventFieldDefinition(
					modelDef,
					type, 
					RuleDefinition.EXPRESSION_VALUE_CODES[valueType], 
					previousLevel, value);
			
			return (result);
		}
	} // getCeIndependentCopy

	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= getSqlRestriction =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public String getSqlRestriction(
			User currentUser, EventObj ce) {
		
		String restriction = "";
			
		// a ce field
		if (value.startsWith("ce.")) {
			
			switch (valueType) {
			case RuleDefinition.STRING_VALUE:
				restriction += "'" + (String) ce.getField(
						modelDef.getFieldId(value)).
						getValue() + "'";
				break;

			case RuleDefinition.BOOLEAN_VALUE:
				restriction += "'1" + 
						(((String) ce.getField(
								modelDef.getFieldId(value)).
								getValue()).equals("true") ? 
								"=" : "<>") + "1'";
				break;

			case RuleDefinition.TIMESTAMP_VALUE:
				restriction += "'" + 
						Utilities.getCompleteFormattedDateString(
								((Long) ce.getField(
										modelDef.getFieldId(value)).
										getValue()).longValue()) + "'";
				break;

			default:
				restriction = value;
				break;
			}
		}
		// a CES field (in the DB)
		else {
			restriction = value.replace(".", "_").substring(1);
		}

		return (restriction);
	} // getSqlRestriction



	
//  ----------------------------------------------------------
//  ------------------------   toString   ------------------------
//  ----------------------------------------------------------
	public String toString() {
		String result = "";
		
		result = value;
		
		return (result);
	}


	public ModelDefinition getModelDef() {
		return modelDef;
	}

}; // EventFieldExpression
